package com.koocloud.electroplatemanage.service.impl;

import cn.hutool.core.lang.Validator;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koocloud.electroplatemanage.auth.mapper.IUserDao;
import com.koocloud.electroplatemanage.common.pojo.ResponseTemplate;
import com.koocloud.electroplatemanage.constant.Constant;
import com.koocloud.electroplatemanage.mapper.DrawNoMapper;
import com.koocloud.electroplatemanage.mapper.IncomePartUnitMapper;
import com.koocloud.electroplatemanage.mapper.TypeWorkMapper;
import com.koocloud.electroplatemanage.mapper.WorkTimeMapper;
import com.koocloud.electroplatemanage.pojo.*;
import com.koocloud.electroplatemanage.service.DrawNoService;
import com.koocloud.electroplatemanage.utils.*;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import static com.koocloud.electroplatemanage.constant.IncomePartJudgeSqlFieldConstant.*;
import static com.koocloud.electroplatemanage.constant.IncomePartJudgeSqlFieldConstant.ZERO;

/**
 * @program: electroplatemanage
 * @description: 图号服务层实现类
 * @author: zww
 * @create: 2020-09-29 16:38
 */
@Service
@Transactional(rollbackFor=Exception.class)
public class DrawNoServiceImpl implements DrawNoService {
    @Resource
    private IUserDao iUserDao;
    @Resource
    private DrawNoMapper drawNoMapper;
    @Resource
    private IncomePartUnitMapper incomePartUnitMapper;
    @Resource
    private TypeWorkMapper typeWorkMapper;
    @Resource
    private WorkTimeMapper workTimeMapper;
    Lock lock=new ReentrantLock();
    /**
     *@Description:通过pk查询一条记录
     *@Param: [pk]
     *@return: com.koocloud.electroplatemanage.common.ResponseTemplate
     *@Author: zww
     *@date: 2020/9/29
     */
    @Override
    public ResponseTemplate selectByPk(String pk) {
        try {
            //新建图号实体
            DrawNoVo drawNoVo = null;
            drawNoVo = drawNoMapper.selectByPk(pk);
            //判断查表操作是否成功
            if (null != drawNoVo) {
                //查表成功返回数据
                return ResponseTemplate.builder().code(0).message("查询成功").count(1l).data(drawNoVo).build();
            }
            else {
                //查表失败返回提示
                return ResponseTemplate.builder().code(1).message("没有此数据").count(0l).build();
            }
        } catch (Exception e) {
            //遇到异常，返回异常原因
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResponseTemplate.builder().code(1).message(e.getMessage()).count(0l).build();
        }
    }
    /**
     *@Description:查询所有记录 分页
     *@Param: [page, limit]
     *@return: com.koocloud.electroplatemanage.common.ResponseTemplate
     *@Author: zww
     *@date: 2020/9/29
     */
    @Override
    public ResponseTemplate selectAllData(String page, String limit,DrawNoVo drawNoVo) {
        //校验分页参数是否为纯数字 并且page和limit不都等于0
        if(Validator.isNumber(page)&&Validator.isNumber(limit)&&!("0".equals(page)&&"0".equals(limit))) {
            try {
                //开始分页
                PageHelper.startPage(Integer.valueOf(page), Integer.valueOf(limit));
                List<DrawNoVo> DrawNoVoList = new ArrayList<>();
                //判断模糊查询是否为状态查询 启用为 1 禁用为 0
                if (!StringUtils.isEmpty(drawNoVo.getIdOrName())){
                    //启用
                    if (STARTUSING.equals(drawNoVo.getIdOrName())){
                        drawNoVo.setIdOrName(ONE);
                        DrawNoVoList = drawNoMapper.selectByDeleteFlag(drawNoVo);
                    //禁用
                    } else if (FORBIDDEN.equals(drawNoVo.getIdOrName())){
                        drawNoVo.setIdOrName(ZERO);
                        DrawNoVoList = drawNoMapper.selectByDeleteFlag(drawNoVo);
                    //模糊查询
                    }else {
                        DrawNoVoList = drawNoMapper.selectAllData(drawNoVo);
                    }
                }else {
                    //全局查询
                    DrawNoVoList = drawNoMapper.selectAllData(drawNoVo);
                }
                //构造分页实体
                PageInfo<DrawNoVo> info=new PageInfo<>(DrawNoVoList);
                if (info.getTotal()>0) {
                    //查询到数据
                    return ResponseTemplate.builder().code(0).message("查询成功").count(info.getTotal()).data(info.getList()).build();
                } else {
                    //没有查询到数据
                    return ResponseTemplate.builder().code(1).message("没有查到对应数据").count(0l).build();
                }
            } catch (Exception e) {
                //遇到异常，返回异常原因
                e.printStackTrace();
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return ResponseTemplate.builder().code(1).message(e.getMessage()).count(0l).build();
            }
        }
        else {
            return ResponseTemplate.builder().code(1).message("分页参数错误").count(0l).build();
        }
    }
    
    /**
     *@Description:插入一条记录
     *@Param: [drawNoVo]
     *@return: com.koocloud.electroplatemanage.common.ResponseTemplate
     *@Author: zww
     *@date: 2020/9/29
     */
    @Override
    public ResponseTemplate insert(DrawNoVo drawNoVo) {
/*        if (!StringUtils.isEmpty(drawNoVo.getDrawNoCode()) && SecondCheck.codeCheck(drawNoVo.getDrawNoCode())) {*/
        try {
            lock.lock();
            //查询图号是否存在
            DrawNoVo drawNoVoByName = drawNoMapper.selectByName(drawNoVo.getName());
            if (!StringUtils.isEmpty(drawNoVoByName)) {
                //存在
                return ResponseTemplate.builder().code(1).message("该图号已存在，或重名！").count(0l).build();
            }else {
                //查询图号表中总数
                int sum = drawNoMapper.selectAmount();
                //获取编号字母的字母下标
                int letterGroup = sum / AMOUNT;
                //获取编号数字
                int numberGroup = sum % AMOUNT;
                String str = null;
                //字母数组
                char[] letters = LETTER.toCharArray();
                //判断表中数据为空时和编号数字在0到999之间
                if ((letterGroup == 0 && numberGroup == 0) || (numberGroup > 0 && numberGroup < AMOUNT)) {
                    str = letters[letterGroup] + String.format("%03d", numberGroup + 1);
                } else {
                    str = letters[letterGroup + 1] + String.format("%03d", 1);
                }
                //判断生成的编码是否存在
                while (true) {
                    DrawNoVo drawNoVoByCode = drawNoMapper.selectByCode(str);
                    //存在跳过，继续生成
                    if (!StringUtils.isEmpty(drawNoVoByCode)) {
                        //字母
                        String startStr = null;
                        //数字
                        String endStr = null;
                        startStr = str.substring(0, 1);
                        endStr = str.substring(1);
                        if (Integer.valueOf(endStr) == AMOUNT) {
                            endStr = String.format("%03d", 1);
                            if (startStr.equals(letters[letterGroup])) {
                                startStr = String.valueOf(letters[letterGroup + 1]);
                            } else {
                                startStr = String.valueOf(letters[letterGroup + 2]);
                            }
                            str = startStr + endStr;
                        } else {
                            endStr = String.format("%03d", Integer.valueOf(endStr) + 1);
                            str = startStr + endStr;
                        }
                    } else {
                        break;
                    }
                }
                drawNoVo.setDrawNoCode(str);
            }
                //存入图号表
            int rows = drawNoMapper.insert(drawNoVo);
/*            int rows = 0;
            DrawNoVo drawNoVo1 = drawNoMapper.selectByCode(drawNoVo.getDrawNoCode());
            if (!StringUtils.isEmpty(drawNoVo1)) {
                return ResponseTemplate.builder().code(1).message("图号编码已存在，请重新录入信息").count(0l).build();
            } else {
                //没有异常并且插入数据条数大于0
                rows = drawNoMapper.insert(drawNoVo);
            }*/
            if (rows > 0) {
                return ResponseTemplate.builder().code(0).message("插入数据成功").count(1l).build();
            } else {//没有异常并且插入数据失败，原则上不出现
                return ResponseTemplate.builder().code(1).message("插入数据失败").count(0l).build();
            }
        } catch (Exception e) {
            //出现异常返回异常原因
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResponseTemplate.builder().code(1).message(e.getMessage()).count(0l).build();
        }
        finally {
            lock.unlock();
        }
/*        }else {
            return ResponseTemplate.builder().code(1).message("图号编码必须以大写字母开头，其余为数字，长度为4位。").count(0l).build();
        }*/
    }

    /**
     *@Description:批量插入记录
     *@Param: [listDrawNo]
     *@return: com.koocloud.electroplatemanage.common.ResponseTemplate
     *@Author: zww
     *@date: 2020/9/29
     */
    @Override
    public ResponseTemplate batchInsert(List<DrawNoVo> listDrawNoVo) {
        if(!listDrawNoVo.isEmpty()) {
            try {
                lock.lock();
                //没有异常并且插入数据条数大于0
                int rows = drawNoMapper.batchInsert(listDrawNoVo);
                if (rows > 0) {
                    return ResponseTemplate.builder().code(0).message("批量插入数据成功").count((long)rows).build();
                } else {//没有异常并且插入数据失败，原则上不出现
                    return ResponseTemplate.builder().code(1).message("批量插入数据失败").count(0l).build();
                }
            } catch (Exception e) {
                //出现异常返回异常原因
                e.printStackTrace();
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return ResponseTemplate.builder().code(1).message(e.getMessage()).count(0l).build();
            }
            finally {
                lock.unlock();
            }
        }
        else {
            return ResponseTemplate.builder().code(1).message("传入参数为空").count(0l).build();
        }
    }
    /**
     *@Description:逻辑删除一条记录
     *@Param: [pk]
     *@return: com.koocloud.electroplatemanage.common.ResponseTemplate
     *@Author: zww
     *@date: 2020/9/29
     */
    @Override
    public ResponseTemplate delete(String pk,String deleteFlag) {
        try {
            lock.lock();
            int rows = drawNoMapper.delete(pk,deleteFlag);
            //判断删除操作是否成功
            if (rows > 0) {
                //删除成功返回数据
                return ResponseTemplate.builder().code(0).message("删除成功").count(1l).build();
            }
            else {
                //删除失败返回提示
                return ResponseTemplate.builder().code(1).message("删除失败").count(0l).build();
            }
        } catch (Exception e) {
            //遇到异常，返回异常原因
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResponseTemplate.builder().code(1).message(e.getMessage()).count(0l).build();
        }
        finally {
            lock.unlock();
        }

    }
    /**
     *@Description:通过pks批量逻辑删除记录
     *@Param: [listPks]
     *@return: com.koocloud.electroplatemanage.common.ResponseTemplate
     *@Author: zww
     *@date: 2020/9/29
     */
    @Override
    public ResponseTemplate batchDeleteByPks(List<String> listPks) {
        if(!listPks.isEmpty()) {
            try {
                lock.lock();
                //存在合法pk值，进行批量删除操作
                int rows = drawNoMapper.batchDeleteByPks(listPks);
                //判断批量删除操作是否成功
                if (rows > 0) {
                    //批量删除成功返回数据
                    return ResponseTemplate.builder().code(0).message("批量删除成功").count((long)rows).build();
                } else {
                    //批量删除失败返回提示
                    return ResponseTemplate.builder().code(1).message("批量删除失败").count(0l).build();
                }
            } catch (Exception e) {
                //遇到异常，返回异常原因
                e.printStackTrace();
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return ResponseTemplate.builder().code(1).message(e.getMessage()).count(0l).build();
            }
            finally {
                lock.unlock();
            }
        }
        else {
            return ResponseTemplate.builder().code(1).message("传入参数为空").count(0l).build();
        }
    }
    /**
     *@Description:修改一条记录
     *@Param: [drawNoVo]
     *@return: com.koocloud.electroplatemanage.common.ResponseTemplate
     *@Author: zww
     *@date: 2020/9/29
     */
    @Override
    public ResponseTemplate update(DrawNoVo drawNoVo) {
        try {
            lock.lock();
            //没有异常并且修改数据条数大于0
            int rows = drawNoMapper.update(drawNoVo);
            if(rows>0) {
                return ResponseTemplate.builder().code(0).message("修改数据成功").count(1l).build();
            }
            else {//没有异常并且修改数据失败，原则上不出现
                return ResponseTemplate.builder().code(1).message("修改数据失败").count(0l).build();
            }
        } catch (Exception e) {
            //出现异常返回异常原因
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResponseTemplate.builder().code(1).message(e.getMessage()).count(0l).build();
        }
        finally {
            lock.unlock();
        }
    }

    /**
     *@Description: 校验填写的图号表主键
     *@Param: [drawNoVo]
     *@return: com.koocloud.electroplatemanage.common.pojo.ResponseTemplate
     *@Author: xxc
     *@date: 2020/10/16
     */
    @Override
    public ResponseTemplate check(DrawNoVo drawNoVo) {
        try {
            log("传入填写的图号表主键："+drawNoVo.getDrawNoCode());

            if (!StringUtils.isEmpty(drawNoVo.getDrawNoCode())){ //判断传入的主键是否为空
                //校验主键的长度
                if (!(drawNoVo.getDrawNoCode().length()==Integer.parseInt(Constant.controlPrikeyLength))){

                    return ResponseTemplate.builder().code(1).message("填写的图号表主键必须长度为"+Constant.controlPrikeyLength+"位").count(0l).build();
                }else { //校验长度通过
                    if (SecondCheck.codeCheck(drawNoVo.getDrawNoCode())) {
                        //根据主键id查询一条数据
                        DrawNoVo drawNoVo1 = drawNoMapper.selectByPk(drawNoVo.getDrawNoCode());
                        if (!StringUtils.isEmpty(drawNoVo1)) {
                            return ResponseTemplate.builder().code(1).message("填写的图号表主键已存在,请重新输入").count(1l).build();
                        } else {
                            return ResponseTemplate.builder().code(0).message("此图号表主键可用").count(1l).build();
                        }
                    }else {
                        return ResponseTemplate.builder().code(1).message("图号编码必须以大写字母开头，其余为数字，长度为4位。").count(0l).build();
                    }
                }
            }else {
                return ResponseTemplate.builder().code(1).message("传入的图号表主键为空").count(1l).build();
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResponseTemplate.builder().code(1).message(e.toString()).count(0l).build();
        }

    }

    /**
     *@Description: 图号模糊搜索
     *@Param: [idOrName]
     *@return: com.koocloud.electroplatemanage.common.pojo.ResponseTemplate
     *@Author: xxc
     *@date: 2020/11/17
     */
    @Override
    public ResponseTemplate selectDrawNo(String idOrName) {
        try {
            List<CodeNameResult> list = drawNoMapper.selectDrawNo(idOrName);
            //判断查表操作是否成功
            if (list.size()>0) {
                //查表成功返回数据
                return ResponseTemplate.builder().code(0).message("查询成功").count(1l).data(list).build();
            }
            else {
                //查表失败返回提示
                return ResponseTemplate.builder().code(1).message("没有此数据").count(0l).build();
            }
        } catch (Exception e) {
            //遇到异常，返回异常原因
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResponseTemplate.builder().code(1).message(e.getMessage()).count(0l).build();
        }
    }

    public void  log(String str){
        System.out.println("====================>"+str+"=================>");
    }


    /**
     *@Description: 图号基础情报excel导入
     *@Param: [idName, file]
     *@return: com.koocloud.electroplatemanage.common.pojo.ResponseTemplate
     *@Author: xxc
     *@date: 2020/11/17
     */
    @Override
    public ResponseTemplate importExcel(String idName, String file) {
        try {
            lock.lock();
            //获取excel数据
            List<List<String>> excelData = ExcelUtil.getData(file);
            if (excelData.size() == 1 && excelData.get(0).size() == 1){
                //错误信息
                String errorInfo = excelData.get(0).get(0);
                return ResponseTemplate.builder().code(1).message(errorInfo).count(0l).build();
            }
            //获取表头
            List<String> headerList = excelData.get(0);

            //记录重复个数
            int count = 0;
            //记录订单重复数
            List<String> repeOrderNumber = new ArrayList<>();

            List<String> list = new ArrayList<>();
            for (String str:headerList){
                //表头去空格
                list.add(StringReplacUtils.replaceBlank(str));
            }
            //获取型号下标
            int modelNumberIndex = 0 ;
            if (list.contains(MODEL_NUMBER)){ modelNumberIndex = list.indexOf(MODEL_NUMBER); }
            //获取料品.料号 下标
            int drawNoIndex = 0 ;
            if (list.contains(DRAWNOAS)){ drawNoIndex = list.indexOf(DRAWNOAS); }
            //获取料品.料品名称 下标
            int partNameIndex = 0 ;
            if (list.contains(PART_NAME_AS)){ partNameIndex = list.indexOf(PART_NAME_AS); }
            //获取工序+工序名称  下标
            int heatSurfaceSequenceIndex = 0 ;
            if (list.contains(HEAT_SURFACE_SEQUENCE_AS)){ heatSurfaceSequenceIndex = list.indexOf(HEAT_SURFACE_SEQUENCE_AS); }
            //获取工时 下标
            int workTimeIndex = 0 ;
            if (list.contains(WORK_TIME)){ workTimeIndex = list.indexOf(WORK_TIME); }
            //获取工种下标
            int typeWorkIndex = 0 ;
            if (list.contains(TYPEWORK)){ typeWorkIndex = list.indexOf(TYPEWORK); }
            //获取操作人下标
            int operatorIndex = 0 ;
            if (list.contains(OPERATOR_USER)){ operatorIndex = list.indexOf(OPERATOR_USER); }
            //抛去表头，从第二行开始
            for (int i = 1 ; i < excelData.size();i++ ){
                List<String> listData = excelData.get(i);
                //工种实体
                TypeWorkVo typeWorkVo = new TypeWorkVo();
                //图号实体
                DrawNoVo drawNoVo = new DrawNoVo();
                //工时实体
                WorkTimeVo workTimeVo = new WorkTimeVo();
                //操作人获取工号
                StringBuffer operator = new StringBuffer();
                if(!StringUtils.isEmpty(listData.get(operatorIndex))){
                    if (listData.get(operatorIndex).trim().contains("、")){
                        String[] splits = listData.get(operatorIndex).trim().split("、");
                        for (int j = 0; j < splits.length; j++) {
                            String jobNum = iUserDao.selectByFullName(splits[j]);
                            if (j == 0){
                                operator.append(jobNum);
                            }else {
                                operator.append("、").append(jobNum);
                            }
                        }
                    }else {
                        operator.append(iUserDao.selectByFullName(listData.get(operatorIndex).trim()));
                    }
                    //操作人
                    typeWorkVo.setOperator(String.valueOf(operator));
                }else {
                    //操作人
                    typeWorkVo.setOperator(String.valueOf(operator));
                }

                //工种逻辑
                if (!StringUtils.isEmpty(listData.get(typeWorkIndex)) &&!StringUtils.isEmpty(listData.get(typeWorkIndex).trim())){
                    //查询工种是否存在
                    TypeWorkVo typeWorkVoByName = typeWorkMapper.selectByName(listData.get(typeWorkIndex).trim());
                    if (!StringUtils.isEmpty(typeWorkVoByName)) {
                        //存在
                        drawNoVo.setTypeWorkCode(typeWorkVoByName.getTypeworkCode());
                        workTimeVo.setTypeWorkCode(typeWorkVoByName.getTypeworkCode());
                    }else {
                        //查询工种表中总数
                        int sum = typeWorkMapper.selectAmount();
                        //获取编号字母的字母下标
                        int letterGroup = sum/AMOUNT;
                        //获取编号数字
                        int numberGroup = sum%AMOUNT;
                        String str = null;
                        //字母数组
                        char[] letters = LETTER.toCharArray();
                        //判断表中数据为空时和编号数字在0到999之间
                        if ((letterGroup==0 && numberGroup ==0) || (numberGroup > 0 && numberGroup<AMOUNT)){
                            str = letters[letterGroup] + String.format("%03d", numberGroup+1);
                        }else {
                            str = letters[letterGroup+1] + String.format("%03d", 1);
                        }
                        //判断生成的编码是否存在
                        while(true){
                            TypeWorkVo typeWorkVoByCode = typeWorkMapper.selectByCode(str);
                            //存在跳过，继续生成
                            if (!StringUtils.isEmpty(typeWorkVoByCode)){
                                //字母
                                String startStr = null;
                                //数字
                                String endStr = null;
                                startStr = str.substring(0,1);
                                endStr = str.substring(1);
                                if (Integer.valueOf(endStr) == AMOUNT ){
                                    endStr = String.format("%03d", 1);
                                    if (startStr.equals(letters[letterGroup])){
                                        startStr = String.valueOf(letters[letterGroup+1]);
                                    }else {
                                        startStr = String.valueOf(letters[letterGroup+2]);
                                    }
                                    str = startStr + endStr;
                                }else {
                                    endStr = String.format("%03d", Integer.valueOf(endStr)+1);
                                    str = startStr + endStr;
                                }
                            } else {
                                break;
                            }
                        }
                        typeWorkVo.setTypeworkCode(str);
                        typeWorkVo.setName(listData.get(typeWorkIndex).trim());
                        //存入工种表
                        typeWorkMapper.insert(typeWorkVo);
                        //设置工种编码
                        drawNoVo.setTypeWorkCode(str);
                        workTimeVo.setTypeWorkCode(str);
                    }
                }

                //获取图号实体
                if (!StringUtils.isEmpty(listData.get(drawNoIndex)) &&!StringUtils.isEmpty(listData.get(drawNoIndex).trim())){
                    //查询图号是否存在
                    DrawNoVo drawNoVoByName = drawNoMapper.selectByName(listData.get(drawNoIndex).trim());
                    if (!StringUtils.isEmpty(drawNoVoByName)) {
                        //存在
                        workTimeVo.setDrawNoCode(drawNoVoByName.getDrawNoCode());
                    }else {
                        //查询图号表中总数
                        int sum = drawNoMapper.selectAmount();
                        //获取编号字母的字母下标
                        int letterGroup = sum/AMOUNT;
                        //获取编号数字
                        int numberGroup = sum%AMOUNT;
                        String str = null;
                        //字母数组
                        char[] letters = LETTER.toCharArray();
                        //判断表中数据为空时和编号数字在0到999之间
                        if ((letterGroup==0 && numberGroup ==0) || (numberGroup > 0 && numberGroup<AMOUNT)){
                            str = letters[letterGroup] + String.format("%03d", numberGroup+1);
                        }else {
                            str = letters[letterGroup+1] + String.format("%03d", 1);
                        }
                        //判断生成的编码是否存在
                        while(true){
                            DrawNoVo drawNoVoByCode = drawNoMapper.selectByCode(str);
                            //存在跳过，继续生成
                            if (!StringUtils.isEmpty(drawNoVoByCode)){
                                //字母
                                String startStr = null;
                                //数字
                                String endStr = null;
                                startStr = str.substring(0,1);
                                endStr = str.substring(1);
                                if (Integer.valueOf(endStr) == AMOUNT ){
                                    endStr = String.format("%03d", 1);
                                    if (startStr.equals(letters[letterGroup])){
                                        startStr = String.valueOf(letters[letterGroup+1]);
                                    }else {
                                        startStr = String.valueOf(letters[letterGroup+2]);
                                    }
                                    str = startStr + endStr;
                                }else {
                                    endStr = String.format("%03d", Integer.valueOf(endStr)+1);
                                    str = startStr + endStr;
                                }
                            } else {
                                break;
                            }
                        }
                        drawNoVo.setDrawNoCode(str);
                        drawNoVo.setModelNumber(listData.get(modelNumberIndex));
                        drawNoVo.setPartName(listData.get(partNameIndex));
                        drawNoVo.setHeatSurfaceSequence(listData.get(heatSurfaceSequenceIndex));
                        drawNoVo.setName(listData.get(drawNoIndex).trim());
                        //存入图号表
                        drawNoMapper.insert(drawNoVo);
                        //设置图号编码
                        workTimeVo.setDrawNoCode(str);
                    }
                }
                //工时逻辑
                if (!StringUtils.isEmpty(listData.get(drawNoIndex)) && !StringUtils.isEmpty(listData.get(typeWorkIndex))){
                    if (!StringUtils.isEmpty(listData.get(drawNoIndex).trim()) && !StringUtils.isEmpty(listData.get(typeWorkIndex))){

                        //查询对应此图号工种的工时是否存在
                        WorkTimeVo workTimeVoByDrawNoTypeWork = workTimeMapper.selectByDrawNoTypeWork(workTimeVo.getDrawNoCode(),workTimeVo.getTypeWorkCode());
                        if (StringUtils.isEmpty(workTimeVoByDrawNoTypeWork)) {
                            //查询工时表中总数
                            int sum = workTimeMapper.selectAmount();
                            //获取编号字母的字母下标
                            int letterGroup = sum/AMOUNT;
                            //获取编号数字
                            int numberGroup = sum%AMOUNT;
                            String str = null;
                            //字母数组
                            char[] letters = LETTER.toCharArray();
                            //判断表中数据为空时和编号数字在0到999之间
                            if ((letterGroup==0 && numberGroup ==0) || (numberGroup > 0 && numberGroup<AMOUNT)){
                                str = letters[letterGroup] + String.format("%03d", numberGroup+1);
                            }else {
                                str = letters[letterGroup+1] + String.format("%03d", 1);
                            }
                            //判断生成的编码是否存在
                            while(true){
                                WorkTimeVo workTimeVoByCode = workTimeMapper.selectByCode(str);
                                //存在跳过，继续生成
                                if (!StringUtils.isEmpty(workTimeVoByCode)){
                                    //字母
                                    String startStr = null;
                                    //数字
                                    String endStr = null;
                                    startStr = str.substring(0,1);
                                    endStr = str.substring(1);
                                    if (Integer.valueOf(endStr) == AMOUNT ){
                                        endStr = String.format("%03d", 1);
                                        if (startStr.equals(letters[letterGroup])){
                                            startStr = String.valueOf(letters[letterGroup+1]);
                                        }else {
                                            startStr = String.valueOf(letters[letterGroup+2]);
                                        }
                                        str = startStr + endStr;
                                    }else {
                                        endStr = String.format("%03d", Integer.valueOf(endStr)+1);
                                        str = startStr + endStr;
                                    }
                                } else {
                                    break;
                                }
                            }
                            workTimeVo.setWorkTimeCode(str);
                            workTimeVo.setWorkTime(listData.get(workTimeIndex));
                            //存入工时表
                            workTimeMapper.insert(workTimeVo);
                        }
                    }
                }
            }
            //删除文件
            DeleteFileUtil.delete(file);
            return ResponseTemplate.builder().code(0).message("导入成功").count(Long.valueOf(count)).data(repeOrderNumber).build();
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResponseTemplate.builder().code(1).message(e.getMessage()).count(0l).build();
        }
        finally {
            lock.unlock();
        }
    }



}